-- =========================================
-- Add Constraint into table MATHANG
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.MATHANG
	ADD CONSTRAINT FK_MH_LMH FOREIGN KEY(LoaiMatHang) REFERENCES dbo.LOAIMATHANG(MaLoai) ON DELETE SET NULL
GO

-- =========================================
-- Add Constraint into table CHIETKHAU
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.CHIETKHAU
	ADD CONSTRAINT FK_CK_MH FOREIGN KEY(MaMatHang) REFERENCES dbo.MATHANG(MaMatHang) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table GIANIEMYET
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.GIANIEMYET
	ADD CONSTRAINT FK_GNY_MH FOREIGN KEY(MaMatHang) REFERENCES dbo.MATHANG(MaMatHang) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table CTPHIEUNHAP
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.CTPHIEUNHAP
	ADD CONSTRAINT FK_CTPN_MH FOREIGN KEY(MaMatHang) REFERENCES dbo.MATHANG(MaMatHang) ON DELETE SET NULL
GO

ALTER TABLE dbo.CTPHIEUNHAP
	ADD CONSTRAINT FK_CTPN_PN FOREIGN KEY(MaPhieu) REFERENCES dbo.PHIEUNHAP(MaPhieu) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table CTMATHANG
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.CTMATHANG
	ADD CONSTRAINT FK_CTMH_MH FOREIGN KEY(MaMatHang) REFERENCES dbo.MATHANG(MaMatHang) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table CTHOADON
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.CTHOADON
	ADD CONSTRAINT FK_CTHD_MH FOREIGN KEY(MaMatHang) REFERENCES dbo.MATHANG(MaMatHang) ON DELETE SET NULL
GO

ALTER TABLE dbo.CTHOADON
	ADD CONSTRAINT FK_CTHD_HD FOREIGN KEY(MaHoaDon) REFERENCES dbo.HOADON(MaHoaDon) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table PHIEUNHAP
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.PHIEUNHAP
	ADD CONSTRAINT FK_PN_NV FOREIGN KEY(NVLapPhieu) REFERENCES dbo.NHANVIEN(MaNhanVien) ON DELETE SET NULL
GO

-- =========================================
-- Add Constraint into table HOADON
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.HOADON
	ADD CONSTRAINT FK_HD_NV FOREIGN KEY(NVBan) REFERENCES dbo.NHANVIEN(MaNhanVien) ON DELETE SET NULL
GO

-- =========================================
-- Add Constraint into table LUONGNV
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.LUONGNV
	ADD CONSTRAINT FK_LNV_NV FOREIGN KEY(MaNhanVien) REFERENCES dbo.NHANVIEN(MaNhanVien) ON DELETE SET NULL
GO

-- =========================================
-- Add Constraint into table NHANVIEN
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.NHANVIEN
	ADD CONSTRAINT FK_NV_LNV FOREIGN KEY(MaLoaiNV) REFERENCES dbo.LOAINHANVIEN(MaLoai) ON DELETE SET NULL
GO

-- =========================================
-- Add Constraint into table TAMUNG
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.TAMUNG
	ADD CONSTRAINT FK_TU_NV FOREIGN KEY(MaNhanVien) REFERENCES dbo.NHANVIEN(MaNhanVien) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table TKNHANVIEN
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.TKDANGNHAP
	ADD CONSTRAINT FK_TK_NV FOREIGN KEY(MaNhanVien) REFERENCES dbo.NHANVIEN(MaNhanVien) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table PHANQUYEN
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.PHANQUYEN
	ADD CONSTRAINT FK_PQ_LNV FOREIGN KEY(MaLoaiNV) REFERENCES dbo.LOAINHANVIEN(MaLoai) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table THUONGNV
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.THUONGNV
	ADD CONSTRAINT FK_TNV_NV FOREIGN KEY(MaNhanVien) REFERENCES dbo.NHANVIEN(MaNhanVien) ON DELETE CASCADE
GO

-- =========================================
-- Add Constraint into table PHATNV
-- =========================================
USE DAILY
GO
ALTER TABLE dbo.PHATNV
	ADD CONSTRAINT FK_PNV_NV FOREIGN KEY(MaNhanVien) REFERENCES dbo.NHANVIEN(MaNhanVien) ON DELETE CASCADE
GO